package org.spark.api.controller;

import org.apache.commons.codec.digest.DigestUtils;
import org.spark.api.constant.DyTable;
import org.spark.api.dao.hbase.AuthorHBaseDao;
import org.spark.api.dao.hbase.DetailsDateProductAwemeDao;
import org.spark.api.entity.Author;
import org.spark.api.entity.DetailsDateProductAweme;
import org.spark.api.util.MsgPackWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;

/**
 * 达人
 * @author chenwy
 */
@RestController
@RequestMapping("api")
public class AuthorController {


    private AuthorHBaseDao dao;

    private DetailsDateProductAwemeDao detailsDateProductAwemeDao;

    @Autowired
    public void setDao(AuthorHBaseDao dao) {
        this.dao = dao;
    }

    @Autowired
    public void setDetailsDateProductAwemeDao(DetailsDateProductAwemeDao detailsDateProductAwemeDao) {
        this.detailsDateProductAwemeDao = detailsDateProductAwemeDao;
    }

    @GetMapping(value="{authorId}")
    public Author get(@PathVariable String authorId){
        return dao.get(DyTable.AUTHOR.getTableName(), DigestUtils.sha1Hex(authorId).getBytes());
    }

    @GetMapping(value="timestamp")
    public long timestamp(){
        return System.currentTimeMillis();
    }

    @GetMapping(value="{date}/{productId}/{awemeId}")
    public DetailsDateProductAweme aweme(@PathVariable  String date, @PathVariable String productId,@PathVariable  String awemeId) throws IOException {
        MsgPackWriter writer = MsgPackWriter.create();
        byte [] rowKey = writer.pack(DigestUtils.sha1Hex(date)).pack(productId).pack(awemeId).toByteArray();
        return detailsDateProductAwemeDao.get(DyTable.DETAILS_DATE_PRODUCT_AWEME.getTableName(), rowKey);
    }

}
